﻿using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook;

namespace PlaxoSyncWM {
    public class IdDeltas {
        private readonly OutlookSession os = new OutlookSession();
        private readonly ArrayList editedContacts = new ArrayList();
        private readonly ArrayList deletedContacts = new ArrayList();
        private readonly ArrayList contactIDsFromFile = new ArrayList();

        public ArrayList IdDeletedContacts() {
            ArrayList fileContactIDs = getContactIDsFromMobileContactsFile();
            foreach (string contactID in fileContactIDs) {
                //look for deleted
                if (contactID != null && !contactExistsOnDevice(contactID))
                    deletedContacts.Add(contactID);
            }
            return deletedContacts;
        }

        public ArrayList IdEditedContacts() {
            ArrayList fileContents;
            Regex splitOnRegX = new Regex(@",", RegexOptions.Compiled);
            fileContents = getMobileContactsFileAsArrayList();
            //look for edits/changes
            foreach (string line in fileContents) {
                string[] elements = splitOnRegX.Split(line);
                string contactID = elements[0];
                if (contactExistsAndHasBeenEdited(contactID, line))
                    editedContacts.Add(contactID);
            }
            return editedContacts;
        }

        public bool contactExistsAndHasBeenEdited(string contactID, string line) {
            bool contactHasBeenEdited = false;
            ContactCollection cons;
            cons = os.Contacts.Items.Restrict("[Oid]=" + Convert.ToInt32(contactID));
            if (cons.Count != 0) {
                string contactFromDevice =
                    String.Format("{0}, {1}, {2}, {3} {4}, {5}", cons[0].ItemId, cons[0].FirstName, cons[0].LastName,
                                  cons[0].MobileTelephoneNumber, cons[0].Email1Address, cons[0].HomeTelephoneNumber);
                if (line.Equals(contactFromDevice))
                    contactHasBeenEdited = false;
                else contactHasBeenEdited = true;
            }
            return contactHasBeenEdited;
        }

        public bool contactExistsOnDevice(string contactID) {
            ContactCollection cons;
            cons = os.Contacts.Items.Restrict("[Oid]=" + Convert.ToInt32(contactID));
            if (cons.Count != 0)
                return true;
            else return false;
        }

        public ArrayList IdNewContactsOnDevice() {
            ArrayList cidsFromFile = getContactIDsFromMobileContactsFile();
            ArrayList newContactIDs = new ArrayList();
            ContactCollection cons = os.Contacts.Items;
            foreach (Microsoft.WindowsMobile.PocketOutlook.Contact deviceContact in cons) {
                if (!cidsFromFile.Contains(deviceContact.ItemId.ToString()))
                    newContactIDs.Add(deviceContact.ItemId.ToString());
            }
            return newContactIDs;
        }

        public ArrayList getContactIDsFromMobileContactsFile() {
            contactIDsFromFile.Clear();
            using (Reader reader = new Reader(Constants.MobileContacts)) {
                MobileContactsFile mcf = new MobileContactsFile();
                Regex splitOnRegX = new Regex(@",", RegexOptions.Compiled);
                if (mcf.mobileContactsFileExists()) {
                    try {
                        string line;
                        while ((line = reader.ReadLine()) != null) {
                            string[] elements = splitOnRegX.Split(line);
                            string contactID = elements[0];
                            contactIDsFromFile.Add(contactID);
                        }
                    }
                    catch (Exception ex) {
                        MessageBox.Show(ex.Message, ex.StackTrace);
                    }
                }
            }
            return contactIDsFromFile;
        }

        public ArrayList getMobileContactsFileAsArrayList() {
            ArrayList mcfArrayList = new ArrayList();
            MobileContactsFile mcf = new MobileContactsFile();
            using (Reader reader = new Reader(Constants.MobileContacts)) {
                if (mcf.mobileContactsFileExists()) {
                    try {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                            mcfArrayList.Add(line);
                    }
                    catch (Exception ex) {
                        MessageBox.Show(ex.Message, ex.StackTrace);
                    }
                }
            }
            return mcfArrayList;
        }
    }
}